package com.qire.manhua.controller;

import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.widget.TextView;

import com.qire.antsrouter.AntsRouter;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.EventController;
import com.qire.manhua.R;
import com.qire.manhua.databinding.FragmentRecommendBinding;
import com.qire.manhua.fragment.RecommendFragment;
import com.qire.manhua.model.viewModel.impl.RecommendViewModel;
import com.qire.manhua.view.CoordinatorBarLayout;
import com.qire.other.recorder.ComicActionRecorder;

import java.util.Arrays;

import androidx.annotation.NonNull;

public class RecommendController extends EventController<RecommendFragment> {

    private RecommendViewModel viewModel;

    public RecommendController(@NonNull RecommendFragment lifecycleOwner) {
        super(lifecycleOwner);
    }

    @Override
    protected void bindView() {
        FragmentRecommendBinding viewDataBinder = owner.viewDataBinder();
        if(ObjectUtil.nonNull(viewDataBinder)) {
            viewDataBinder.setRecommendController(this);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        this.viewModel = null;
    }

    public void onAddition(RecommendViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /*************************************************************************************************************************/

    private void navBarGradient(int alpha) {
        // todo 上线前临时补丁式处理，在新作页面头部背景有问题的情况，目前发现是由于fragment状态缓存的问题导致的布局使用错误，
        // 使用了女生频道的布局结果导致ID属性任然存在脏数据，需要排查或者修改fragment的加载方式， 这里可以看看 fragment工厂
        if(lastPosition == 5) alpha = 255;

        int   btnTaskImageRid = R.drawable.app_home_recommend_icon_task_a;
        int   btnMagImageRid  = R.drawable.app_home_recommend_icon_message_a;
        int[] tabTextColors   = new int[6];

        int     searchButtonIcon;
        int     searchButtonBgAlpha;
        String  searchButtonTextColor;

        if(alpha >= 128){
            btnTaskImageRid = R.drawable.app_home_recommend_icon_task_b;
            btnMagImageRid  = R.drawable.app_home_recommend_icon_message_b;
            Arrays.fill(tabTextColors, Color.rgb( 0x13, 0x13, 0x13));
            tabTextColors[lastPosition] = Color.BLACK;

            searchButtonIcon = R.drawable.app_common_icon_search_grey;
            searchButtonBgAlpha = 0xff;
            searchButtonTextColor = "#aeaeae";
        }else{
            Arrays.fill(tabTextColors, Color.WHITE);

            searchButtonIcon = R.drawable.app_common_icon_search_white;
            searchButtonBgAlpha = 0x33;
            searchButtonTextColor = "#ffffff";
        }

        viewModel.setTopBarAlpha(alpha);
        viewModel.setTaskIconRid(btnTaskImageRid);
        viewModel.setMessageIconRid(btnMagImageRid);

        viewModel.setSearchButtonIcon(searchButtonIcon);
        viewModel.setSearchButtonBgAlpha(searchButtonBgAlpha);
        viewModel.setSearchButtonTextColor(searchButtonTextColor);

        FragmentRecommendBinding viewDataBinder = owner.viewDataBinder();
        for(int index = 0; index < 6; index++) {
            TextView tabText = (TextView) viewDataBinder.recommendTabLayout.getTabAt(index);
            tabText.setTextColor(tabTextColors[index]);
            tabText.setTextSize(index == lastPosition ? 17 : 15);
            tabText.setTypeface(index == lastPosition ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT);
        }
    }

    public void coordinatorBarOnHeaderMoveChanged(View view, boolean isDragging, int offSet) {
        CoordinatorBarLayout appBarLayout = ObjectUtil.as(view, CoordinatorBarLayout.class);
        if(ObjectUtil.nonNull(appBarLayout)) {
            int alpha = 255 - Math.min(255, offSet * 2);
            appBarLayout.setAlpha((float) alpha / 255f);
        }
    }
    /**
     * 协调BarLayout监听，用于控制上下滑动式联动动画渐变操作
     * @param appBarLayout
     * @param verticalOffset
     */
    public void coordinatorBarOnLayoutMoveChanged(View appBarLayout, int verticalOffset) {
        int appBarLayoutOffset = -verticalOffset / 2;
        int offsetY = Math.max(0, appBarLayoutOffset);
        int alpha   = Math.min(255, offsetY);
        navBarGradient(alpha);
    }

    private int lastPosition = 0;
    private int lastOffsetPixels = 0;
    private boolean isLeft = false;
    /**
     * tab选项卡标题滑动监听，用于控制左右滑动时标题栏渐变操作
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    public void tabLayoutOnPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        FragmentRecommendBinding viewDataBinder = owner.viewDataBinder();
        TextView tabTextNew = (TextView) viewDataBinder.recommendTabLayout.getTabAt(position);
        TextView tabTextOld = (TextView) viewDataBinder.recommendTabLayout.getTabAt(lastPosition);

        if(positionOffsetPixels == 0 && position != lastPosition) {
            tabTextNew.setTextSize(17);
            tabTextNew.setTypeface(Typeface.DEFAULT_BOLD);
            tabTextOld.setTextSize(15);
            tabTextOld.setTypeface(Typeface.DEFAULT);
            lastPosition = position;

            // todo 上线前临时补丁式处理，在新作页面头部背景有问题的情况，目前发现是由于fragment状态缓存的问题导致的布局使用错误，
            // 使用了女生频道的布局结果导致ID属性任然存在脏数据，需要排查或者修改fragment的加载方式， 这里可以看看 fragment工厂
            if(lastPosition == 5) navBarGradient(255);
        } else {
            isLeft = lastOffsetPixels < positionOffsetPixels;
            lastOffsetPixels = positionOffsetPixels;

            if (isLeft) {
                // 可再这里进行渐变动画调整
            } else {

            }
        }
    }

    public void onSearchButtonClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.ComicSearchPage).navigation();
        // 行为数据统计
        ComicActionRecorder.build(ComicActionRecorder.count_search_click)
                .setSource("首页")
                .doRecorde(view.getContext());
    }

    public void onTaskButtonClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.TaskCenterPage).navigation();
    }

    public void onMessageButtonClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.MessageCenterPage).navigation();
    }

    public void floatGuideLoginCloseBtnOnClick(View view) {
        viewModel.closeFloatGuideLoginLayer();
    }

    public void floatGuideLoginBtnOnClick(View view) {
        AntsRouter.ROUTER.buildCard(Navigation.Page.AccountBindGuidePage).navigation();
    }

}
